<?
class imci_cougn extends module {

    // Author: Herman Tolentino MD
    // CHITS Project 2004
    // for module guidelines see MODULES.TXT
    // specific for consults

    function imci_cough() {
    //
    // constructor
    // do not forget to update version
    //
        $this->author = 'Herman Tolentino MD';
        $this->version = "0.1-".date("Y-m-d");
        $this->module = "imci_cough";
        $this->description = "Health Center Module - IMCI Cough";
    }

    // --------------- STANDARD MODULE FUNCTIONS ------------------

    function init_deps() {
    //
    // insert dependencies in module_dependencies
    // use multiple inserts
    //
        module::set_dep($this->module, "module");
        module::set_dep($this->module, "healthcenter");

    }

    function init_lang() {
    //
    // insert necessary language directives
    //
        module::set_lang("LBL_NOTES_HISTORY", "english", "HISTORY", "Y");
        module::set_lang("LBL_NOTES_PHYSICALEXAM", "english", "PHYSICAL EXAM", "Y");
        module::set_lang("LBL_NOTES_PLAN", "english", "PLAN", "Y");

    }

    function init_menu() {
        // use this for updating menu system
        // under LIBRARIES
        if (func_num_args()>0) {
            $arg_list = func_get_args();
        }
        // _<modulename> in SQl refers to function _<modulename>() below
        // _barangay in SQL refers to function _barangay() below;

        // put in more details
        module::set_detail($this->description, $this->version, $this->author, $this->module);

    }

    function init_stats() {
    }

    function init_help() {
    }

    function init_sql() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
        }

        module::execsql("CREATE TABLE `m_imci_cough` (".
            "`consult_id` float NOT NULL default '0',".
            "`duration_days` int(11) NOT NULL default '0',".
            "`tachypnea` char(1) NOT NULL default '',".
            "`chest_indrawing` char(1) NOT NULL default '',".
            "`stridor` char(1) NOT NULL default '',".
            "`classify` char(1) NOT NULL default '',".
            "`cough_timestamp` timestamp(14) NOT NULL,".
            "`user_id` int(11) NOT NULL default '0',".
            "PRIMARY KEY  (`consult_id`)".
            ") TYPE=InnoDB; ");

    }

    function drop_tables() {
        $sql = "DROP TABLE `m_imci_cough`;";
        module::execsql($sql);
    }

    // --------------- CUSTOM MODULE FUNCTIONS ------------------

    function display_notes() {
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
        }
        print "<table width='400'>";
        print "<tr valign='top'><td colspan='3'>";
        print "<span class='library'>".FTITLE_TEMPLATE_LIST."</span><br>";
        print "</td></tr>";
        print "<tr valign='top'><td><b>".THEAD_TEMPLATE_KEY."</b></td><td><b>".THEAD_TEMPLATE_TEXT."</b></td></tr>";
        $sql = "select template_key, template_text from m_template order by template_key";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $text) = mysql_fetch_array($result)) {
                    print "<tr valign='top'><td><a href='".$_SERVER["SELF"]."?page=LIBRARIES&menu_id=$menu_id&barangay_id=$id'>$id</a></td><td>$text</td></tr>";
                }
            }
        }
        print "</table><br>";
    }

    function process_notes() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        if ($post_vars["submittemplate"]) {
            if ($post_vars["template_key"] && $post_vars["template_text"]) {
                switch($post_vars["submittemplate"]) {
                case "Add Template":
                    $sql = "insert into m_template (template_key, template_text) ".
                           "values ('".$post_vars["template_key"]."', '".$post_vars["template_text"]."')";
                    $result = mysql_query($sql);
                    break;
                case "Update Template":
                    $sql = "update m_template set ".
                           "template_text = '".$post_vars["template_text"]."' ".
                           "where template_key = '".$post_vars["template_key"]."'";
                    $result = mysql_query($sql);
                    break;
                case "Delete Template":
                    $sql = "delete from m_template where template_key = '".$post_vars["template_key"]."'";
                    $result = mysql_query($sql);
                    break;
                }
            }
        }
    }

    function load_template() {
        if (func_num_args()) {
            $arg_list = func_get_args();
            $template_key = $arg_list[0];
        }
        $sql = "select template_text from m_template where template_key = '$template_key'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($text) = mysql_fetch_array($result);
                return $text;
            }
        }
    }

    function form_imci_cough() {
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            if ($get_vars["template_key"]) {
                $sql = "select template_key, template_text ".
                       "from m_template where template_key = '".$get_vars["template_key"]."'";
                if ($result = mysql_query($sql)) {
                    if (mysql_num_rows($result)) {
                        $template = mysql_fetch_array($result);
                    }
                }
            }
        }
        print "<table width='400'>";
        print "<form action = '".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id' name='form_notes' method='post'>";
        print "<tr valign='top'><td>";
        print "<br>";
        print "<span class='boxtitle'>".LBL_NOTES_HISTORY."</span><br> ";
        $default_history = notes::load_template("notes_history");
        print "<textarea class='textbox' rows='8' cols='30' name='notes_history' style='border: 1px solid #000000'>".($notes["notes_history"]?$notes["notes_history"]:($post_vars["notes_history"]?$post_vars["notes_history"]:$default_history))."</textarea><br>";
        print "<small>Use <b>notes_history</b> in template library.</small><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_NOTES_PHYSICALEXAM."</span><br> ";
        $default_physicalexam = notes::load_template("notes_physicalexam");
        print "<textarea class='textbox' rows='8' cols='30' name='notes_physicalexam' style='border: 1px solid #000000'>".($notes["notes_physicalexam"]?$notes["notes_physicalexam"]:($post_vars["notes_physicalexam"]?$post_vars["notes_physicalexam"]:$default_physicalexam))."</textarea><br>";
        print "<small>Use <b>notes_physicalexam</b> in template library.</small><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_NOTES_PLAN."</span><br> ";
        $default_physicalexam = notes::load_template("notes_plan");
        print "<textarea class='textbox' rows='8' cols='30' name='notes_plan' style='border: 1px solid #000000'>".($notes["notes_physicalexam"]?$notes["notes_physicalexam"]:($post_vars["notes_physicalexam"]?$post_vars["notes_physicalexam"]:$default_physicalexam))."</textarea><br>";
        print "<small>Use <b>notes_plan</b> in template library.</small><br/>";
        print "</td></tr>";
        print "<tr><td><br>";
        if ($get_vars["template_key"]) {
            print "<input type='hidden' name='notes_id' value='".$get_vars["notes_id"]."'>";
            print "<input type='submit' value = 'Update Notes' class='textbox' name='submitnotes' style='border: 1px solid #000000'> ";
            print "<input type='submit' value = 'Delete Notes' class='textbox' name='submitnotes' style='border: 1px solid #000000'> ";
        } else {
            print "<input type='submit' value = 'Add Notes' class='textbox' name='submitnotes' style='border: 1px solid #000000'> ";
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";
    }

    function _imci_cough() {
    //
    // main method for notes
    // calls form_notes, process_notes, display_notes
    //
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
        }
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('template')) {
            return print($exitinfo);
        }

        if ($post_vars["submitbarangay"]) {
            $this->process_notes($menu_id, $post_vars, $get_vars);
            header("location: ".$_SERVER["PHP_SELF"]."?page=LIBRARIES&menu_id=$menu_id");
        }
        $this->display_notes($menu_id);
        $this->form_notes($menu_id, $post_vars, $get_vars);
    }

}
?>
